package com.bootdo.essential.controller;

import com.bootdo.common.annotation.IpFilter;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.essential.domain.BankDO;
import com.bootdo.essential.serivce.BankService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.bootdo.common.utils.ShiroUtils.getUserId;

/**
 * @Desc 用户银行管理
 * @date 2019/12/23
 *
 */
@Controller
@RequestMapping("/essential/bank")
public class BankController {

    @Autowired
    private BankService bankService;

    private final String PREFIX = "essential/bank";

    @ResponseBody
    @PostMapping("/queryByUser")
    public R queryByUser(){
        BankDO bank = bankService.getByUser(getUserId());
        if (bank == null){
            return R.error(-1, "当前没有收款银行信息");
        } else {
            return R.ok();
        }
    }

    @GetMapping()
    @RequiresPermissions("essential:bank:bank")
    String bank(){
        return PREFIX + "/list";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("essential:bank:bank")
    public PageUtils list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        List<BankDO> bankList = bankService.list(query);
        int total = bankService.count(query);
        PageUtils pageUtils = new PageUtils(bankList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("essential:bank:add")
    String add(Model model){
        return PREFIX + "/add";
//        //如果当前用户存在收款账号则直接跳转到编辑页面
//        BankDO bank = bankService.getByUser(getUserId());
//        if (bank != null){
//            model.addAttribute("bank", bank);
//            return PREFIX + "/edit";
//        } else {
//            return PREFIX + "/add";
//        }
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("essential:bank:edit")
    String edit(@PathVariable("id") Long id, Model model){
        BankDO bank = bankService.get(id);
        model.addAttribute("bank", bank);
        return PREFIX + "/edit";
    }

    /**
     * 保存
     */
    @IpFilter
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("essential:bank:add")
    public R save(BankDO bank){
        if(bankService.save(bank)>0){
            return R.ok();
        }
        return R.error();
    }
    /**
     * 修改
     */
    @IpFilter
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("essential:bank:edit")
    public R update( BankDO bank){
        bankService.update(bank);
        return R.ok();
    }

    /**
     * 删除
     */
    @IpFilter
    @PostMapping( "/remove")
    @ResponseBody
    @RequiresPermissions("essential:bank:remove")
    public R remove( Long id){
        if(bankService.remove(id)>0){
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @IpFilter
    @PostMapping( "/batchRemove")
    @ResponseBody
    @RequiresPermissions("essential:bank:batchRemove")
    public R remove(@RequestParam("ids[]") Long[] ids){
        bankService.batchRemove(ids);
        return R.ok();
    }
}
